
import matplotlib.pyplot as plt
import re
import numpy as np

# def parse_result(path):
# 	f=open(path)
# 	lines = f.readlines()
# 	f.close()
# 	for i in range(1,len(lines)+1):
# 		line = lines[-i]
# 		mat = re.match("\('exec_count', (\d+)?\)",line)
# 		if mat:
# 			exec_count = int(mat.groups()[0])
# 			break
# 	ts = []
# 	for line in lines:
# 		mat = re.match("update consume time=(.+)?",line)
# 		if mat:
# 			graph_consume_time = float(mat.groups()[0])
# 			ts.append(graph_consume_time)
# 	ret = exec_count,ts
# 	assert len(ts)==1000
# 	return ret


# def get_data(prefix):
# 	ret =[[],[]]
# 	for i in range(1,11):
# 		exec_count,ts = parse_result("result/test2_result2/Geant-2012-10host-lambda-%d%s.txt"%(i,prefix))
# 		ret[0].append(exec_count)
# 		ret[1].append(np.average(ts))
# 	return ret

# nomerge = get_data("")
# merge = get_data("-merge")


nomerge = [[1382, 1382, 1382, 1382, 1382, 1382, 1382, 1382, 1382, 1382], [0.14950420379640103, 0.16255173444744797, 0.198128149509405, 0.24349037408830201, 0.337543776035171, 0.668169577121321, 0.9632148456570259, 1.3898905324934614, 1.8801013636588317, 2.472945177555422]]
merge = [[1382, 1382, 1382, 1382, 1362, 1262, 1190, 1107, 1077, 1034], [0.15257287502288996, 0.16238714694976, 0.20599766731264002, 0.258947789668989, 0.30363582277294, 0.513784797191667, 0.6435967087745391, 0.5603171038631201, 0.6968403339388489, 1.085756475925151]]
print(nomerge)
print(merge)


lam = [i/10.0 for i in range(1,11)]

width=2.8
height=3.0*2.6/4

fig = plt.gcf()
fig.set_size_inches(width,height)

def cacl(a,b):
	return (a-b)/float(a)

print(cacl(nomerge[0][9],merge[0][9]))
print(cacl(nomerge[1][9],merge[1][9]))

plt.plot(lam,nomerge[0],"r.-",label="Sequential")
plt.plot(lam,merge[0],"b^-",label="Consus")
plt.xlabel("$\lambda$ ($s^{-1}$)")
plt.ylabel("Operation Number")
plt.grid(linestyle='--')
plt.legend()
# plt.show()

fig.savefig("figs/operation-number.pdf",dpi=400,bbox_inches="tight")

plt.clf()

height-=0.16

fig = plt.gcf()
fig.set_size_inches(width,height)
plt.plot(lam,nomerge[1],"r.-",label="Sequential")
plt.plot(lam,merge[1],"b^-",label="Consus")
plt.xlabel("$\lambda$ ($s^{-1}$)")
plt.ylabel("Time (s)")

ax = plt.gca()
nnn = np.linspace(0.5,2.5,5)
ax.set_yticks(nnn)
la = [str(a) for a in nnn]
ax.set_yticklabels(la)
plt.grid(linestyle='--')
plt.legend()
# plt.show()
fig.savefig("figs/averagetime.pdf",dpi=400,bbox_inches="tight")
